module material.gentextures;

import canvas.abstractcanvas;
import canvas.floatcanvas3d;
import std.stdio;
import std.math;
import utils.color;
import utils.perlin;
import utils.vector2;
import utils.vector3;

void genTexChecker(AbstractCanvas dest, int gridSize) {
    for (int x = 0; x < dest.width; x++) {
        for (int y = 0; y < dest.height; y++) {
            float col = 1.0f*((x/gridSize+y/gridSize) % 2 == 0);
            dest.drawPixel(Vector2i(x,y),RGBColor(col,col,col));
        }
    }
}

void genTexNoise(AbstractCanvas dest, int level) {
    float nmin = float.max;
    float nmax = float.min;
    for (int x = 0; x < dest.width; x++) {
        for (int y = 0; y < dest.height; y++) {
            //float col = noise(cast(float)(level*x)/dest.width,cast(float)(level*y)/dest.height,0)/2.0f+0.5f;
            float col = turbulence2(cast(float)(x*level)/dest.width,cast(float)(y*level)/dest.height,2,2,1);
            if (col > nmax)
                nmax = col;
            if (col < nmin)
                nmin = col;
            dest.drawPixel(Vector2i(x,y),RGBColor(col,col,col));
        }
    }
    writefln("%f, %f",nmin,nmax);
}

void genTexWood(FloatCanvas3D dest, int level) {
    RGBColor wood_col1 = RGBColor(2.0f/3.0f,1.0f/3.0f,2.0f/15.0f);
    RGBColor wood_col2 = RGBColor(14.0f/15.0f,0.6f,2.0f/15.0f);

    float nmin = float.max;
    float nmax = float.min;
    for (int x = 0; x < dest.width; x++) {
        for (int y = 0; y < dest.height; y++) {
            for (int z = 0; z < dest.depth; z++) {
                float col = noise(cast(float)(level*x)/dest.width,cast(float)(level*y)/dest.height,cast(float)(level*z)/dest.depth)/2.0f+0.5f;
                col *= 20;
                col -= floor(col);
                if (col > nmax)
                    nmax = col;
                if (col < nmin)
                    nmin = col;
                RGBColor cout = wood_col1 + col*(wood_col2-wood_col1);
                dest.drawPixel(Vector3i(x,y,z),cout);
            }
        }
    }
    writefln("%f, %f",nmin,nmax);
}

void genTexMarble(FloatCanvas3D dest, int level) {
    float nmin = float.max;
    float nmax = float.min;
    for (int x = 0; x < dest.width; x++) {
        for (int y = 0; y < dest.height; y++) {
            for (int z = 0; z < dest.depth; z++) {
                float col = cos(5*2*PI*cast(float)x/dest.width+noise(cast(float)(level*x)/dest.width,cast(float)(2*level*y)/dest.height,cast(float)(2*level*z)/dest.depth)*5)/2.0f+0.5f;
                if (col > nmax)
                    nmax = col;
                if (col < nmin)
                    nmin = col;
                RGBColor cout = RGBColor(col,col,col);
                dest.drawPixel(Vector3i(x,y,z),cout);
            }
        }
    }
    writefln("%f, %f",nmin,nmax);
}

